package com.yongcheon.sms_manager.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamRegistDlg;
import com.yongcheon.sms_manager.ui.preference.SpamNumCursorAdapter;
import com.yongcheon.sms_manager.vo.SpamNumVO;

/**
 * Created by yongcheon on 2014-12-09.
 */
public class SpamNumManagementActivity extends Activity {
    private SpamNumCursorAdapter cursorAdapter;
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setTitle(this.getResources().getString(R.string.spam_number_management));
        setContentView(R.layout.spam_num_list);
        setAdapter();
        setActionBar();

    }

    public void setAdapter(){
        ListView listView = (ListView)findViewById(R.id.spam_num_list);
        SpamNumberData spamNumberData = new SpamNumberData(this);
        SQLiteDatabase database = SpamDBManager.getDatabase(this);
        Cursor cursor = spamNumberData.select(null, this, database);
        cursorAdapter = new SpamNumCursorAdapter(this, cursor);
        cursorAdapter.setSpamNumberData(spamNumberData);
        listView.setAdapter(cursorAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_preference_spam_num, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.add_custom_spam_num :
                ListView listView = (ListView)findViewById(R.id.spam_num_list);
                SpamNumVO spamNumVO = new SpamNumVO();
                spamNumVO.setCategory_1(this.getResources().getString(R.string.user_regist));
                SpamRegistDlg.showSpamRegistDlg(this, listView, spamNumVO, this, true);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void setActionBar(){
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    public void registSpam(SpamNumVO spamNumVO){
        if(spamNumVO.getAddress() == null || spamNumVO.getAddress().trim().equals("")){
            Toast.makeText(this, this.getResources().getString(R.string.spam_number_is_empty), Toast.LENGTH_SHORT).show();
            return;
        }
        SpamNumberData spamNumData = new SpamNumberData(this);
        SQLiteDatabase database = SpamDBManager.getDatabase(this);
        spamNumData.insertNotExist(spamNumVO, this, database);
        cursorAdapter.getCursor().requery();
    }
}
